﻿using System;
using System.Windows;
using System.Windows.Threading;
using BrainTechLLC;
using ImageAppViewModels;

namespace ShowMultiple.Plugin
{
    public partial class ShowMultipleImagesWindow : Window
    {
        public event EventHandler<ObjectEventArgs<BitmapsAndDescriptionsViewModel>> ClosedWindow;

        public static readonly DependencyProperty BitmapsAndDescriptionsViewModelsProperty =
            DependencyProperty.Register("BitmapsAndDescriptionsViewModels", typeof(BitmapsAndDescriptionsViewModel), typeof(ShowMultipleImagesWindow), new PropertyMetadata(null, OnBitmapsAndDescriptionsViewModel1PropertyChanged));

        public BitmapsAndDescriptionsViewModel BitmapsAndDescriptionsViewModels
        {
            get { return (BitmapsAndDescriptionsViewModel)GetValue(BitmapsAndDescriptionsViewModelsProperty); }
            set { SetValue(BitmapsAndDescriptionsViewModelsProperty, value); }
        }

        private static void OnBitmapsAndDescriptionsViewModel1PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ShowMultipleImagesWindow source = d as ShowMultipleImagesWindow;
            source.UpdateBitmapsAndDescriptionsViewModel1(e.NewValue as BitmapsAndDescriptionsViewModel, e.OldValue as BitmapsAndDescriptionsViewModel);
        }

        private void UpdateBitmapsAndDescriptionsViewModel1(BitmapsAndDescriptionsViewModel viewModel, BitmapsAndDescriptionsViewModel old)
        {
            if (viewModel != old)
            {
                BitmapsAndDescriptionsViewModels = viewModel;

                if (old != null)
                {
                    old.Clear();
                }

                this.DataContext = BitmapsAndDescriptionsViewModels;
            }
        }


        public ShowMultipleImagesWindow()
        {
            InitializeComponent();
            Closing += new System.ComponentModel.CancelEventHandler(ShowImagesWindow_Closing);            
        }
        
        void ShowImagesWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Closing -= ShowImagesWindow_Closing;

            if (ClosedWindow != null)
                ClosedWindow(this, new ObjectEventArgs<BitmapsAndDescriptionsViewModel>(BitmapsAndDescriptionsViewModels));

            if (BitmapsAndDescriptionsViewModels != null)
            {
                BitmapsAndDescriptionsViewModels.Clear();
                BitmapsAndDescriptionsViewModels = null;
            }
        }

        private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Escape)
            {
                e.Handled = true;
                this.Close();
            }
        }

        private void widthHeight_ChangeSize(object sender, ObjectEventArgsNonRef<double, double> e)
        {
            show.ChangeSize(e.obj1, e.obj2);
        }

        private void show_Loaded(object sender, RoutedEventArgs e)
        {
            DispatcherTimer dt = new DispatcherTimer()
            {
                Interval = new TimeSpan(0, 0, 1),
                IsEnabled = true,
            };
            dt.Tick += new EventHandler(dt_Tick);
            dt.Start();
        }

        void dt_Tick(object sender, EventArgs e)
        {
            DispatcherTimer dt = sender as DispatcherTimer;

            if (show._wrap != null)
            {
                widthHeight.SetLastWidthHeight((int)show._wrap.ItemWidth, (int)show._wrap.ItemHeight);
                dt.IsEnabled = false;
                dt.Stop();
            }
        }

    }
}
